<?php

if (!defined('PATH'))
	exit();

class Store extends Controller {

	protected $default_action = 'browse';
	public $_title = 'Store';

	public function __construct() {
		parent::__construct();
	}

	public function action_browse() {
		$account = $this->archez->getLogged();
		$view = new View('store/index');
		$storeItems = new Model_StoreItems();
		$view->account = $account;
		$view->storeItems = $storeItems;
		$view->render();
	}

	public function action_check() {
		if (!isset($this->paramsURI[2])) {
			$this->redirect(PATH . '/store/');
		}

		if (!is_numeric($this->paramsURI[2])) {
			$this->redirect(PATH . '/store/');
		}

		$item = new Model_Store();
		$item->load($this->paramsURI[2]);
		if ($item->isLoaded()) {
			$view = new View('store/check_form');
			$view->check = $item;
			$view->render();
		} else {
			$this->archez->showMsg('Item doesn\'t exist, <a href="' . PATH . '/store/' . $this->default_action . '">click here</a> to go back', 'error');
		}
	}

	public function action_buy() {
		if (LOGGED) {
			if (!isset($this->paramsURI[2])) {
				$this->redirect(PATH . '/store/');
			}

			if (!is_numeric($this->paramsURI[2])) {
				$this->redirect(PATH . '/store/');
			}

			$item = new Model_Store();
			$item->load($this->paramsURI[2]);
			if ($item->isLoaded()) {
				$account = $this->archez->getLogged();
				$view = new View('store/buy_form');
				$view->account = $account;
				$view->check = $item;
				$view->render();
			} else {
				$this->archez->showMsg('Item doesn\'t exist, <a href="' . PATH . '/store/' . $this->default_action . '">click here</a> to go back', 'error');
			}
		} else {
			$this->redirect(PATH . '/account/login/store/' . $this->paramsURI[2]);
		}
	}

	public function action_confirm() {
		if (LOGGED) {
			$account = $this->archez->getLogged();
			if (isset($_POST['itemid'])) {
				$item = new Model_Store();
				$item->load($_POST['itemid']);
				if (!$item->isLoaded()) {
					$this->archez->showMsg('Item doesn\'t exist, <a href="' . PATH . '/store/' . $this->default_action . '">click here</a> to go back', 'error');
				} else {
					$storeRecord = new Model_StoreRecord();
					if (isset($_POST['character'])) {
						if ($_POST['other']) {
							if (!isset($_POST['from']) || empty($_POST['from'])) {
								$this->archez->showMsg('Please fill all fields, <a href="' . PATH . '/store/' . $this->default_action . '">click here</a> to go back', 'error');
							} else if (!$this->archez->checkBaseName(Helper_General::clean(ucfirst($_POST['from'])))) {
								$this->archez->showMsg('Wrong format of field \'from who\', <a href="' . PATH . '/store/' . $this->default_action . '">click here</a> to go back', 'error');
							} else {
								$name = stripslashes(strip_tags($_POST['other']));
								$player = $this->ots->createObject('Player');
								$player->find($name);
								if ($player->isLoaded()) {
									if ($player->getAccount() != $account->getId()) {
										if ($account->getPremiumPoints() >= $item->price) {
											$account->setPremiumPoints($account->getPremiumPoints() - $item->price);
											$account->save();
											if ($item->type == Model_Store::TYPE_ITEM) {
												$storeRecord->fromacc = $account->getId();
												$storeRecord->fromguid = 0;
												$storeRecord->toguid = $player->getId();
												$storeRecord->itemid = $item->itemid;
												$storeRecord->container = $item->container;
												$storeRecord->count = $item->amount;
												$storeRecord->subcount = $item->subamount;
												$storeRecord->status = Model_StoreRecord::STATUS_NOTDELIVERED;
												$storeRecord->type = Model_Store::TYPE_ITEM;
												$storeRecord->offer_id = $item->id;
												$storeRecord->fromname = Helper_General::clean(ucfirst($_POST['from']));
												$storeRecord->save();
												$this->archez->showMsg('You have successfully sent the item with name: <b>' . $item->name . '</b> to ' . $player->getName() . ', <a href="' . PATH . '/store/' . $this->default_action . '">click here</a> to proceed.', 'success');
											} else if ($item->type == Model_Store::TYPE_PREMMY) {
												$this->db->query('UPDATE `accounts` SET `premdays` = `premdays` + ' . $this->db->quote($item->amount) . ' WHERE `id` = ' . $player->getAccount());
												$storeRecord->fromacc = $account->getId();
												$storeRecord->fromguid = 0;
												$storeRecord->toguid = $player->getId();
												$storeRecord->itemid = 0;
												$storeRecord->container = 0;
												$storeRecord->count = $item->amount;
												$storeRecord->subcount = 0;
												$storeRecord->status = Model_StoreRecord::STATUS_DELIVERED;
												$storeRecord->type = Model_Store::TYPE_PREMMY;
												$storeRecord->offer_id = $item->id;
												$storeRecord->fromname = Helper_General::clean(ucfirst($_POST['from']));
												$storeRecord->save();
												$this->archez->showMsg('You have successfully added ' . $item->amount . ' premium days to another account, <a href="' . PATH . '/store/' . $this->default_action . '">click here</a> to proceed.', 'success');
											}
										} else {
											$this->archez->showMsg('Your account premium ' . $item->unit . ' amount is lower than ' . $item->price . ', <a href="' . PATH . '/store/' . $this->default_action . '">click here</a> to go back.', 'error');
										}
									} else {
										$this->archez->showMsg('The character ' . $player->getName() . ' is in your account, <a href="' . PATH . '/store/' . $this->default_action . '">click here</a> to go back.', 'error');
									}
								} else {
									$this->archez->showMsg('The character name you submitted doesn\'t exist, <a href="' . PATH . '/store/' . $this->default_action . '">click here</a> to go back.', 'error');
								}
							}
						} else {
							if ($item->type == Model_Store::TYPE_PREMMY) {
								if ($account->getPremiumPoints() >= $item->price) {
									$account->setPremiumPoints($account->getPremiumPoints() - $item->price);
									$account->save();
									$this->db->query('UPDATE `accounts` SET `premdays` = `premdays` + ' . $this->db->quote($item->amount) . ' WHERE `id` = ' . $account->getId());
									$storeRecord->fromacc = $account->getId();
									$storeRecord->fromguid = 0;
									$storeRecord->toguid = 0;
									$storeRecord->itemid = 0;
									$storeRecord->container = 0;
									$storeRecord->count = $item->amount;
									$storeRecord->subcount = 0;
									$storeRecord->status = Model_StoreRecord::STATUS_DELIVERED;
									$storeRecord->type = Model_Store::TYPE_PREMMY;
									$storeRecord->offer_id = $item->id;
									$storeRecord->save();
									$this->archez->showMsg('You have successfully added ' . $item->amount . ' premium days to your account, <a href="' . PATH . '/store/' . $this->default_action . '">click here</a> to proceed.', 'success');
								} else {
									$this->archez->showMsg('Your account premium ' . $item->unit . ' amount is lower than ' . $item->price . ', <a href="' . PATH . '/store/' . $this->default_action . '">click here</a> to go back.', 'error');
								}
							} else if ($item->type == Model_Store::TYPE_ITEM) {
								$player = $this->ots->createObject('Player');
								$player->find($_POST['character']);
								if ($player->isLoaded()) {
									if ($player->getAccount() == $account->getId()) {
										if ($account->getPremiumPoints() >= $item->price) {
											$account->setPremiumPoints($account->getPremiumPoints() - $item->price);
											$account->save();

											$storeRecord->fromacc = $account->getId();
											$storeRecord->fromguid = 0;
											$storeRecord->toguid = $player->getId();
											$storeRecord->itemid = $item->itemid;
											$storeRecord->container = $item->container;
											$storeRecord->count = $item->amount;
											$storeRecord->subcount = $item->subamount;
											$storeRecord->status = Model_StoreRecord::STATUS_DELIVERED;
											$storeRecord->type = Model_Store::TYPE_ITEM;
											$storeRecord->offer_id = $item->id;
											$storeRecord->save();
											$this->archez->showMsg('You have successfully bought the item: <b>' . $item->name . '</b> for ' . $player->getName() . ', <a href="' . PATH . '/store/' . $this->default_action . '">click here</a> to proceed.', 'success');
										} else {
											$this->archez->showMsg('Your account premium ' . $item->unit . ' amount is lower than ' . $item->price . ', <a href="' . PATH . '/store/' . $this->default_action . '">click here</a> to go back.', 'error');
										}
									} else {
										$this->archez->showMsg('The character ' . $player->getName() . ' is not in your account, <a href="' . PATH . '/store/' . $this->default_action . '">click here</a> to go back.', 'error');
									}
								} else {
									$this->redirect(PATH . '/store/' . $this->default_action);
								}
							}
						}
					} else {
						$this->redirect(PATH . '/store/' . $this->default_action);
					}
				}
			} else {
				$this->redirect(PATH . '/store/' . $this->default_action);
			}
		} else {
			$this->archez->showMsg('You must <a href="' . PATH . '/account/login/store">log in</a> first.', 'error');
		}
	}

	public function action_points() {
		if (LOGGED) {
			$account = $this->archez->getLogged();
			$view = new View('store/buy_points');
			$view->account = $account;
			$view->render();
		} else {
			$this->archez->showMsg('You must <a href="' . PATH . '/account/login/store/points">log in</a> first.', 'error');
		}
	}

	public function action_record() {
		$this->_title .= ' - Transactions'; // you can edit title everywhere you want to.
		if (LOGGED) {
			$account = $this->archez->getLogged();
			$view = new View('store/record');
			$storeRecordList = new Model_StoreRecordList();
			$view->account = $account;
			$view->storeRecordList = $storeRecordList;
			$item = new Model_Store();
			$view->item = $item;
			$view->render();
		} else {
			$this->archez->showMsg('You must <a href="' . PATH . '/account/login/store/points">log in</a> first.', 'error');
		}
	}

}

?>